home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / DEBUG / OBJTEST / OBJUNIT.PAS < prev   
Pascal/Delphi Source File  |  1996-06-06  |  4KB  |  124 lines

  1. unit Objunit;
  2.  
  3. interface
  4.     uses classes, stdctrls, sysutils;
  5.  
  6. type
  7.   {Its usually a good idea to start from TComponent if methods are involved.
  8.   True? Why?}
  9.     TMyObjectClass = class(TComponent)
  10.       private
  11.         FNeedToCalc:            boolean;             {True = Mean needs to be recalculated}
  12.         FOnDataChange:        TNotifyEvent; {The memo contents changed}
  13.         FMean:                        double;       {The current value of the mean}
  14.  
  15.       function GetMean: double;
  16.  
  17.       public
  18.             {Generally, all data should be private and access methods provided.
  19.       However, in this case if one wanted the user to have complete control
  20.       over the memo, it might be OK.}
  21.            FMemo:                        TMemo;
  22.  
  23.            constructor create(Owner: TComponent); override;
  24.         destructor Destroy; override;
  25.       procedure MyChange(Senter: TObject);
  26.       property Mean: double read GetMean;
  27.       property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange;
  28.  
  29.       {Making this function public allows access by the application program.
  30.       See Comments on Calc button.}
  31.              function CalcMean(Data: TStringList): double;
  32.  end;
  33.  
  34. implementation
  35.     uses main;
  36.  
  37. {''----------------------------------------------------------------------------}
  38. constructor TMyObjectClass.Create(Owner: TComponent);
  39. begin
  40.     inherited Create(Owner);
  41.   FMemo := TMemo.Create(Self);
  42.   FMemo.TabStop := false;
  43.  
  44.   {1. When the Memo's OnChange event fires, it causes the procedure MyChange to
  45.   execute.}
  46.   FMemo.OnChange := MyChange;
  47. end;
  48.  
  49. {''----------------------------------------------------------------------------}
  50. procedure TMyObjectClass.MyChange(Senter: TObject);
  51. begin
  52.     {The contents of the memo has changed, so need to recalculate the mean}
  53.   FNeedToCalc := true;
  54.  
  55.     {2. If FOnDataChange points to a procedure, then its executed. In this case,
  56.   when the object is created, it is set equal to TfrmMain.OnDataChange}
  57.     if Assigned(FOnDataChange) then FOnDataChange(Self);
  58. end;
  59.  
  60. {''----------------------------------------------------------------------------}
  61. destructor TMyObjectClass.Destroy;
  62. begin
  63.     FMemo := nil;
  64.     FMemo.Free;
  65.   inherited Destroy;
  66. end;
  67.  
  68. {''----------------------------------------------------------------------------}
  69. {This function is executed everytime the Mean is retrived. That is, before the
  70. Mean is returned, it is recalculated. But only if FNeedToCalc is true.}
  71. function TMyObjectClass.GetMean: double;
  72. begin
  73.   if FNeedToCalc then begin
  74.       {Typecast the memo lines as a TStringlist. This makes CalcMean somewhat more
  75.     generic.}
  76.     FMean := CalcMean(TStringList(FMemo.Lines));
  77.     FNeedToCalc := false;
  78.   end;
  79.   Result := FMean;
  80. end;
  81.  
  82. {''----------------------------------------------------------------------------}
  83. {Compute the mean of all the strings in a TStringList}
  84. function TMyObjectClass.CalcMean
  85. (
  86.   Data:            TStringList  {Each item in the list is considered a value}
  87. ):                     double;      {Returns the mean of the above values}
  88.  
  89. var
  90.     i:                integer;
  91.   Count:        integer;
  92.   Sum:            Double;
  93.   Mean:            Double;
  94.  
  95. begin
  96.     Sum :=        0;
  97.   Count :=    0;
  98.  
  99.     {Go through all the lines in the stringlist}
  100.     for i := 0 to Data.Count - 1 do begin
  101.       try
  102.         Sum := Sum + StrToFloat(Data[i]);
  103.       Inc(Count);
  104.     except
  105.         {If the line cannot be converted to a number, ignore it and go to the
  106.       next line.}
  107.         on EConvertError do begin
  108.           continue;
  109.       end;
  110.     end;
  111.   end;
  112.  
  113.   if Count > 0 then begin
  114.       Mean := Sum / Count;
  115.   end else begin
  116.     Mean := 0;
  117.   end;
  118.  
  119.   Result := Mean;
  120.  
  121. end;
  122.  
  123. end.
  124.